﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BCMS.FrontEnd.DataContract;
using BCMS.Business.Implementation;
using CrystalDecisions.Shared;

namespace BCMS.Forms.Report
{
    public partial class frmReleaseReport : Form
    {
        public string ReportName { set; get; }
        public frmMain FrmParent { set; get; }
        public frmReleaseReport()
        {
            InitializeComponent();
        }

        public frmReleaseReport(string type, frmMain main)
        {
            this.Font = SystemFonts.MessageBoxFont;
            InitializeComponent();

            this.ReportName = type;
            this.FrmParent = main;
            loadKindList();
            loadTargetList();
        }

        private void loadTargetList()
        {
            if (cbTarget.Items.Count.Equals(0))
            {
                List<KeyValuePair<int, string>> targets = new List<KeyValuePair<int, string>>();
                cbTarget.Items.Clear();
                targets.Add(new KeyValuePair<int, string>(0, "Tất cả"));
                targets.Add(new KeyValuePair<int, string>((int)RTarget.CUSTOMER, "Khách hàng"));
                targets.Add(new KeyValuePair<int, string>((int)RTarget.PARTNER, "Đối tác"));
                targets.Add(new KeyValuePair<int, string>((int)RTarget.STAFF, "Nhân viên"));
                cbTarget.DataSource = targets;
                cbTarget.ValueMember = "Key";
                cbTarget.DisplayMember = "Value";
                cbTarget.SelectedIndex = 0;
            }

        }

        private void loadKindList()
        {
            if (cbKind.Items.Count.Equals(0))
            {
                List<KeyValuePair<int, string>> kinds = new List<KeyValuePair<int, string>>();
                cbKind.Items.Clear();
                kinds.Add(new KeyValuePair<int, string>(0, "Tất cả"));
                kinds.Add(new KeyValuePair<int, string>((int)RKind.SELL, "Bán"));
                kinds.Add(new KeyValuePair<int, string>((int)RKind.DISCOUNT, "Chiết khấu"));
                kinds.Add(new KeyValuePair<int, string>((int)RKind.GIFT, "Quà tặng"));
                cbKind.DataSource = kinds;
                cbKind.ValueMember = "Key";
                cbKind.DisplayMember = "Value";
                cbKind.SelectedIndex = 0;
            }
        }

        private void btnView_Click(object sender, EventArgs e)
        {
            List<ReleaseUI> src = new List<ReleaseUI>();
            DALclient client = new DALclient();

            switch (ReportName)
            {
                case "TotalRelease":
                    src = client.searchRelease(string.Empty, 0,
                                                txtParValue.Value, (int)cbKind.SelectedValue,
                                                (int)cbTarget.SelectedValue, txtDetail.Text,
                                                dtFrom.Value.Date, dtTo.Value.Date.AddDays(1));
                    break;
            }

            if (src != null)
            {
                SortableBindingList<ReleaseUI> list = new SortableBindingList<ReleaseUI>(src);
                FrmParent.grvReport.DataSource = list;
                FrmParent.formatReleaseGrid(FrmParent.grvReport);
            }
            else
                FrmParent.grvReport.DataSource = null;

            FrmParent.updateReleaseSummary();
            this.Close();
        }

        private void btnPrintPreview_Click(object sender, EventArgs e)
        {
            List<ReleaseUI> src = new List<ReleaseUI>();
            DALclient client = new DALclient();

            switch (ReportName)
            {
                case "TotalRelease":
                    src = client.searchRelease(string.Empty, 0,
                                                txtParValue.Value, (int)cbKind.SelectedValue,
                                                (int)cbTarget.SelectedValue, txtDetail.Text,
                                                dtFrom.Value.Date, dtTo.Value.Date.AddDays(1));

                    if (src != null)
                    {
                        rptReleaseInfo rptAcc = new rptReleaseInfo();
                        rptAcc.Load(Application.StartupPath + "\\Report\\rptReleaseInfo.rpt");
                        rptAcc.SetDataSource(src);

                        ParameterFields rParms = new ParameterFields();
                        ParameterField parm = new ParameterField();
                        ParameterDiscreteValue disValue = new ParameterDiscreteValue();
                        parm.Name = "FromDate";
                        disValue.Value = dtFrom.Value.Date;
                        parm.CurrentValues.Add(disValue);
                        rParms.Add(parm);

                        ParameterField parmTo = new ParameterField();
                        disValue = new ParameterDiscreteValue();
                        parmTo.Name = "ToDate";
                        disValue.Value = dtTo.Value.Date;
                        parmTo.CurrentValues.Add(disValue);
                        rParms.Add(parmTo);

                        frmReport report = new frmReport(rptAcc, rParms);
                        report.Show();
                    }
                    else
                        MessageBox.Show("Nothing to print", "Oops!");

                    break;
            }
        }
    }
}
